<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Admin\Controller\InitController;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
class AdvertController extends InitController {
    public function _initialize() {
    	parent::_initialize();
        $this->categorys = getcache('category', 'commons');
        $this->modelid = 4;
        $this->models = getcache('model', 'commons');
        $this->userid = (int) session('_userid');
        $this->db = D('AdvertLog');
        /* 将页码变量赋值到模板 */
    }

    /* 打码记录管理 */
    public function manage() {
        $status_conf = array(
            '-1' =>'失败',
            '0'  =>'待审',
            '1'  =>'通过',
            '2'  =>'完成',
        );
        $page = I('page', '0', 'int');
        $count = $this->db->count();
        $infos = $this->db->order("status ASC, id DESC")->page($page, 10)->select();
        $accounts = array();
        if ($infos) {
            $document_ids = $member_ids = array();
            foreach ($infos as $key => $value) {
                $document_ids[] = $value['ad_id'];
                $member_ids[] = $value['uid'];
            }
            $model = $this->models[$this->modelid];
            $document_ids = array_unique($document_ids);
            $document_map = array();
            $document_map['id'] = array("IN", $document_ids);
            $document_info = M($model['tablename'])->where($document_map)->getField('id, title');
            $member_ids = array_unique($member_ids);
            $member_map = array();
            $member_map['userid'] = array("IN", $member_ids);
            $member_info = M('Member')->where($member_map)->getField('userid, username, nickname');            
        }
        $pages = page($count, 10);
        $show_header = FALSE;
        include $this->admin_tpl('advert_manage');
    }

    public function status() {
        if (submitcheck('dosubmit', 'GP')) {
            $param = I('param.');
            extract($param);
            $rs = $this->db->getById($id);
            if(!$rs) {
                $this->error('记录不存在');
            }
            if ($rs['status'] != 0) {
                $this->error('该任务已审核，不能重复审核');
            }
            $model = $this->models[$this->modelid];
            $document_info = M($model['tablename'])->getById($rs['ad_id']);
            if (!$document_info) {
                $this->error('任务不存在');
            }
            $result = $this->db->where(array('id' => $id))->save(array('status' => $state, 'updatetime' => NOW_TIME));
            if ($result) {
                $this->success('操作成功');
                /* 审核通过增加 */
                // if ($state == 1) {
                //     if ((int) $document_info['do_coin'] > 0) {
                //         M('Member')->where(array('userid' => $rs['uid']))->setInc("u_coin", $document_info['do_coin']);
                //         score_change($rs['uid'], '体验广告奖励('.$document_info['title'].')', $document_info['do_coin'], 'u_coin', '1', id_encode($this->modelid, $rs['ad_id']));
                //     }
                //     if ((int) $document_info['do_bean'] > 0) {
                //         M('Member')->where(array('userid' => $rs['uid']))->setInc("u_bean", $document_info['do_bean']);
                //         score_change($rs['uid'], '体验广告奖励('.$document_info['title'].')', $document_info['do_bean'], 'u_bean', '1', id_encode($this->modelid, $rs['ad_id']));
                //     }                    
                // }
            } else {
                $this->error('审核操作失败，请联系管理员');
            }
        } else {
            $this->error('请勿非法提交');
        }
    }

    /* 删除 */
    public function delete() {
        if (submitcheck('dosubmit')) {
            $ids = (array) $_POST['id'];
            if(empty($ids)) $this->error('请选择要删除的项目');
            $sqlmap = array();
            $sqlmap['id'] = array("IN", $ids);
            $this->db->where($sqlmap)->delete();
            $this->success('操作成功');
        } else {
            $this->error('请勿非法操作');
        }
    }
}